import enums.CustomStyles;

import mx.controls.Button;
import mx.controls.textClasses.TextRange;
import mx.managers.PopUpManager;


private function customize():void {
    addCustomButtons();
    var tf:TextField = textArea.mx_internal::getTextField() as TextField;  
    tf.addEventListener(TextEvent.LINK, linkHandler);      		
}

private function addCustomButtons():void{
	var button:Button = new Button();
   		button.name = "addLinkButton";
        button.label = "Add Link...";
        button.enabled = false;                
        button.addEventListener("click", addLinkDialog);
        this.toolbar.addChild(button);  
        this.toolbar.removeChild(this.linkTextInput);  
}


private function checkSelection():void{
	var tf:TextField = textArea.mx_internal::getTextField() as TextField;
	
	var beginIndex:int = tf.selectionBeginIndex;
	var endIndex:int = tf.selectionEndIndex;			
	
	
	if (beginIndex == endIndex) (this.toolbar.getChildByName("addLinkButton") as Button).enabled = false;
	else (this.toolbar.getChildByName("addLinkButton") as Button).enabled = true;
}

private function mouseDownHandler(event:MouseEvent):void{
	checkSelection();	
}

private function linkHandler(linkEvent:TextEvent):void {
	gotoLink(linkEvent.text);
}

private function gotoLink(string:String):void{
	//Insert code that will split the string on ";;" switch on the type and then open the specified file
	trace(string);
}

public function addLink(textRange:TextRange, type:String, linkItem:*):void{	
	var link:String = "<a href=\"event:" + type + ";;"+ linkItem.id + "\">" + textRange.text + "</a>";
	textRange.htmlText = link; 
	textRange.color = CustomStyles.LINK;
}

public function addLinkDialog(event:Event):void {	
    var w:AddLinkDialog = AddLinkDialog(
        PopUpManager.createPopUp(this, AddLinkDialog, true)
    );
 
   	w.textEditor = this;
    PopUpManager.centerPopUp(w);
}
